﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BDH.PRE.Form
{
    using BDH.DAL.Entities;
    public partial class FrmPhong_Popup : DevExpress.XtraEditors.XtraForm
    {
        BLL.PhongBLL _bll = new BLL.PhongBLL();
        public int id_nhatro; public int id_phong;
        public bool IsAdd;  public bool IsEdit;
        FrmInput frm_base = new FrmInput();
        
        public FrmPhong_Popup()
        {
            InitializeComponent();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var ten = frm_base.Proper(txtTenphong.Text);

            if (IsAdd)
            {
                var o = new Phong()
                {
                    tenphong = ten,
                    soluong = Convert.ToInt32("0" + txtSoluong.EditValue),
                    gia = Convert.ToDecimal("0" + txtGia.EditValue),
                    nhatro_id = id_nhatro
                };

                var oki = _bll.Insert(o);
                if (oki)
                {
                    Load_Data();
                    ResetText();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

            if (IsEdit)
            {
                var _id = Convert.ToInt32(gvPhong.GetFocusedRowCellValue("id"));

                var o = new Phong()
                {
                    id = _id,
                    tenphong = ten,
                    soluong = Convert.ToInt32("0" + txtSoluong.EditValue),
                    gia = Convert.ToDecimal("0" + txtGia.EditValue)
                };

                var oki = _bll.Update(o);
                if (oki)
                {
                    Load_Data();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void FrmPhong_Popup_Load(object sender, EventArgs e)
        {
            Load_Data();
            ResetText();
            if (IsEdit)
            {
                var dtb = _bll.Select_By_IdPhong(id_phong);
                txtTenphong.EditValue = dtb.Rows[0]["tenphong"];
                txtSoluong.EditValue = dtb.Rows[0]["soluong"];
                txtGia.EditValue = dtb.Rows[0]["gia"];
                txtTenphong.Focus();
            }
        }

        private void Load_Data()
        {
            var dtb = _bll.Select(id_nhatro);
            if (dtb != null)
                dgPhong.DataSource = dtb;
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        private void ResetText()
        {
            txtTenphong.EditValue = null;
            txtGia.EditValue = null;
            txtSoluong.EditValue = null;
            txtTenphong.Focus();

            base.ResetText();
        }
    }
}